package com.squirrel.common;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Collections;

/**
 * @Description CodeGenerator代码生成器
 * @Author 关远键
 * @Version v1.0.0
 * @Date 2021/11/9 10:10
 */
@Deprecated
@SpringBootTest
public class CodeGenerator {

    private static final String URL = "jdbc:mysql://42.192.167.72:3306/tem_video";
    private static final String USERNAME = "GYJ";
    private static final String PASSWORD = "Gyj@0000";
    private static final String PROJECT_PATH = System.getProperty("user.dir");

    @Test
    void test() {
        FastAutoGenerator.create(URL, USERNAME, PASSWORD)
                .globalConfig(builder -> {
                    builder.author("关远键")          // 设置作者
                            .enableSwagger()        // 开启 swagger 模式
//                            .fileOverride()         // 覆盖已生成文件
                            .disableOpenDir()       // 禁止打开输出目录
                            .dateType(DateType.TIME_PACK)   // 时间类型对应策略
                            .commentDate("yyyy/MM/dd HH:mm")    // 注释日期格式化
                            .outputDir(PROJECT_PATH + "/src/main/java");     // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.squirrel")  // 设置父包名
                            .moduleName("common")   // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, PROJECT_PATH + "/src/main/resources/mapper")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude("user_comment",
                            "user_subscription",
                            "user_video",
                            "video",
                            "banner",
                            "channel",
                            "comment",
                            "commentary_subtitles")  // 设置需要生成的表名
                            // （1）entity策略配置
                            .entityBuilder()
                            //.disableSerialVersionUID()
                            //.enableChainModel()
                            //.superClass(Serializable.class)
                            .enableLombok()
                            .enableRemoveIsPrefix()
                            .enableTableFieldAnnotation()
                            .enableActiveRecord()
                            .versionColumnName("version")
                            .versionPropertyName("version")
                            .logicDeleteColumnName("deleted")
                            .logicDeletePropertyName("deleteFlag")
                            .naming(NamingStrategy.underline_to_camel)
                            .columnNaming(NamingStrategy.underline_to_camel)
                            .addTableFills(new Column("create_time", FieldFill.INSERT))
                            .addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE))
                            .idType(IdType.ASSIGN_UUID)
                            .formatFileName("%s")
                            // （2）mapper策略配置
                            .mapperBuilder()
                            .superClass(BaseMapper.class)
                            .enableMapperAnnotation()
                            .enableBaseResultMap()
                            .enableBaseColumnList()
                            //.cache(MyMapperCache.class)
                            .formatMapperFileName("%sMapper")
                            .formatXmlFileName("%sMapper")
                            // （3）service策略配置
                            .serviceBuilder()
                            .superServiceClass(IService.class)
                            .superServiceImplClass(ServiceImpl.class)
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImp")
                            // （4）controller策略配置
                            .controllerBuilder()
                            //.superClass(BaseController.class)
                            .enableHyphenStyle()
                            .enableRestStyle()
                            .formatFileName("%sController")
                            .build();
//                            .addTablePrefix("t_", "c_");    // 设置过滤表前缀

                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();

    }
}
